package com.iweb.newa202104.controller;

import com.iweb.newa202104.dto.QuestionInputDTO;
import com.iweb.newa202104.enums.Err;
import com.iweb.newa202104.exception.CommonException;
import com.iweb.newa202104.service.QuestionService;
import com.iweb.newa202104.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: LGZ
 * @Date: 2021/5/11 19:23
 * @Description:
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    QuestionService questionService;

    @PostMapping("/create")
    public ResultVO create(@Validated @RequestBody QuestionInputDTO questionInputDTO) {
        return questionService.createQuestion(questionInputDTO);
    }

    @GetMapping("/getQuestionByUser")
    public ResultVO getQuestionByUser(HttpSession httpSession) {
        return questionService.getQuestionByUser(httpSession);
    }

    @GetMapping("/getMyQuestionCounts")
    public ResultVO getMyQuestionCounts(HttpSession httpSession) {
        return questionService.getMyQuestionCounts(httpSession);
    }

    @GetMapping("/getQuestionsByCategory")
    public ResultVO getQuestionsByCategory(String categoryId) {
        try {
            return questionService.getQuestionsByCategory(Integer.parseInt(categoryId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @GetMapping("/getAllQuestion")
    public ResultVO getAllQuestion() {
        return questionService.getAllQuestion();
    }

    @PostMapping("/acceptResponse")
    public ResultVO acceptResponse(@Validated @RequestBody QuestionInputDTO questionInputDTO) {
        return questionService.acceptResponse(questionInputDTO);
    }

    @DeleteMapping("/deleteQuestion")
    public ResultVO deleteQuestion(HttpSession httpSession, String questionId) {
        try {
            return questionService.deleteQuestion(httpSession, Integer.parseInt(questionId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @PostMapping("/giveLike")
    public ResultVO giveLike(@Validated @RequestBody QuestionInputDTO questionInputDTO) {
        return questionService.giveLike(questionInputDTO);
    }

}
